package cannot.backcontroller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cannot.pojo.Permission;
import cannot.pojo.Role;
import cannot.service.PermissionService;
import cannot.service.RolePermissionService;
import cannot.service.RoleService;

@Controller
public class RolePermissionController {
	@Autowired
	PermissionService permissionService;
	@Autowired
	RoleService roleService;
	@Autowired
	RolePermissionService rolePermissionService;
	
	/**
	 * 角色权限管理→权限管理
	 */
	@RequestMapping("roleper_permission_page")
	public String roleperPermissionPage(Model model){
		List<Permission> permissions = permissionService.list();
		model.addAttribute("permissions", permissions);
		return "admin/rightContent/subPermission";
	}
	//删除权限
	@RequestMapping("permission_delete")
	@ResponseBody
	public Object permissionDelete(Long id){
		Permission permission = permissionService.get(id);
		permissionService.delete(id);
		return permission;
	}
	//增加权限
	@RequestMapping("permission_add")
	@ResponseBody
	public Object permissionAdd(Permission permission){
		permissionService.add(permission);
		return permission;
	}
	//修改权限
	@RequestMapping("permission_update")
	@ResponseBody
	public Object permissionUpdate(Permission permission){
		permissionService.update(permission);
		return permission;
	}
	
	/**
	 * 角色权限管理→角色管理
	 */
	@RequestMapping("roleper_role_page")
	public String roleperRolePage(Model model){
		List<Role> roles = roleService.list();
		List<Permission> permissions = permissionService.list();
		model.addAttribute("permissions", permissions);
		model.addAttribute("roles", roles);
		return "admin/rightContent/subRole";
	}
	//删除角色
	@RequestMapping("role_delete")
	@ResponseBody
	public Object roleDelete(Long id){
		Role role = roleService.get(id);
		roleService.delete(id);
		return role;
	}
	//增加角色
	@RequestMapping("role_add")
	@ResponseBody
	public Object roleAdd(Role role){
		roleService.add(role);
		return role;
	}
	//修改角色
	@RequestMapping("role_update")
	@ResponseBody
	public Object roleUpdate(Role role){
		roleService.update(role);
		return role;
	}
	//根据角色id获取权限列表
	@RequestMapping("roleper_getPermission_byRole")
	@ResponseBody
	public Object getPermissionByRole(Long id){
		Role role = new Role();
		role.setId(id);
		List<Permission> permissions = permissionService.list(role);
		return permissions;
	}
	//更新角色权限
	@RequestMapping("roleper_updateRolePermission")
	@ResponseBody
	public Object updateRolePermission(long roleId,long[] permissionId){
		Role role = new Role();
		role.setId(roleId);
		rolePermissionService.setPermissions(role, permissionId);
		return "SUCCESS";
	}
	
	
}
